package com.xboss.tv;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hejunlin.tvsample.widget.AutoLayoutManager;
import com.hejunlin.tvsample.widget.MetroViewBorderImpl;
import com.xboss.tv.adapter.MenuAdapter;
import com.xboss.tv.bean.Contacts;
import com.xboss.tv.ui.ItemContentActivity;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private MetroViewBorderImpl mMetroViewBorderImpl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {

        mMetroViewBorderImpl = new MetroViewBorderImpl(this);
        mMetroViewBorderImpl.setBackgroundResource(R.drawable.border_color);

        mRecyclerView = (RecyclerView) findViewById(R.id.main_view_recycler_menu);

        GridLayoutManager gridlayoutManager = new AutoLayoutManager(this, 4);
        gridlayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(gridlayoutManager);
        mRecyclerView.setFocusable(false);
        mMetroViewBorderImpl.attachTo(mRecyclerView);

        MenuAdapter adapter = new MenuAdapter(MainActivity.this);

        adapter.setOnMenuitemClickListener(new MenuAdapter.OnMenuitemClickListener() {
            @Override
            public void onItemClick(String name, int position) {

                handleMessage(position);
            }
        });

        mRecyclerView.setAdapter(adapter);
        mRecyclerView.scrollToPosition(0);
    }

    private void handleMessage(int position) {//跳转到频道内容页面

        if (!Contacts.isLoad) {
            Intent intent = new Intent(MainActivity.this, ItemContentActivity.class);
            intent.putExtra("position", position);
            startActivity(intent);
            Contacts.isLoad = true;
        }
    }
}
